package band.wukong.practice.redis.jedis;

import band.wukong.util.PropertiesUtil;

import java.util.Properties;

/**
 * @author wukong(wukonggg@139.com)
 */
public class SimpleRedisConfLoader {

    public static final String ENV_MAIN= "main";
    public static final String ENV_TEST= "test";

    private static final String ENV_MAIN_PATH = "src/main/resources/redis.properties";
    private static final String ENV_TEST_PATH = "src/test/resources/redis.properties";

    private static RedisConf conf;

    public static RedisConf load(String env) {
        if (null != conf) {
            return conf;

        }

        Properties props;
        if (ENV_MAIN.equals(env)) {
            props = PropertiesUtil.loadByFileInputStream(ENV_MAIN_PATH);

        } else if (ENV_TEST.equals(env)) {
            props = PropertiesUtil.loadByFileInputStream(ENV_TEST_PATH);

        } else {
            throw new IllegalArgumentException();
        }

        RedisConf conf = new RedisConf();
        conf.setHost(String.valueOf(props.get(RedisConf.HOST)));
        conf.setPort(Integer.valueOf(String.valueOf(props.get(RedisConf.PORT))));
        conf.setPasswd(String.valueOf(props.get(RedisConf.PASSWD)));
        conf.setClusterHostAndIp(String.valueOf(props.get(RedisConf.CLUSTER_HOSTANDIP)));

        return conf;
    }

    private static RedisConf loadCluster(String env) {

        return conf;
    }
}
